package com.saunter.controller;

import com.saunter.model.Salary;
import com.saunter.service.SalaryService;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;

@Api(tags = "管理员编辑数据记录")
@RestController
@RequestMapping("/admin")
public class SalaryController {
    @Autowired
    private SalaryService salaryService;
    @Resource
    private ExceptionManager exceptionManager;

    @ApiOperation("添加记录")
    @PostMapping("/addRecordSingle")
    public Map<String, Object> addSalary(@RequestParam("id_num")String user_id_num, @RequestParam("date")long salary_date, @ApiParam(value = "记录所属日期",required = true)@RequestParam("salary_data")String salary_data, @ApiParam(hidden = true)@RequestParam(value = "status", defaultValue = "1")int status) {
        if(1 == salaryService.addSingle(new Salary(null, user_id_num, salary_data, new Date(salary_date), new Timestamp(System.currentTimeMillis()), status))){
            return Response.success("添加成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("修改记录")
    @PostMapping("/editRecordSingle")
    public Map<String, Object> editSalary(@RequestParam("id")Integer id, @RequestParam(value = "date", required = false)Long salary_date, @ApiParam("记录所属日期")@RequestParam(value = "salary_data", required = false)String salary_data, @ApiParam("1:正常;2:隐藏")@RequestParam(value = "status", required = false)Integer status) {
        if(1 == salaryService.editSalary(new Salary(id, null, salary_data, null == salary_date ? null : new Date(salary_date), null, status))) {
            return Response.success("修改成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("删除记录")
    @GetMapping("/deleteRecordSingle/{salary_id}")
    public Map<String, Object> deleteSalary(@ApiParam(value = "多个id用,连接", required = true)@PathVariable("salary_id")String salary_ids) {
        int res = 0;
        try {
            int salary_id = Integer.parseInt(salary_ids);
            res = salaryService.deleteById(salary_id);
        }catch (Exception e) {
            List<String> salary_ids_int = Arrays.asList(salary_ids.split(","));
            res = salaryService.deleteByIds(salary_ids_int);
        }

        if(1 == res) {
            return Response.success("删除成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("获取分页记录")
    @GetMapping("/getRecord/{page_no}/{page_size}")
    public  Map<String, Object> getRecord(@PathVariable("page_no")int page_no, @PathVariable("page_size")int page_size,
                                          @RequestParam(value = "id_num", required = false)String id_num,@ApiParam("记录的最小添加时间")@RequestParam(value = "time_start", required = false)String add_time_start,@ApiParam("记录的最大添加时间")@RequestParam(value = "time_end", required = false)String add_time_end,
                                          @ApiParam("记录的最小所属日期")@RequestParam(value = "date_start", required = false)String date_start,@ApiParam("记录的最大所属日期")@RequestParam(value = "date_end", required = false)String date_end,@RequestParam(value = "status", required = false)Integer status,
                                          @RequestParam(value = "department_id", required = false)Integer department_id,@RequestParam(value = "job_id", required = false)Integer job_id,@RequestParam(value = "user_name", required = false)String user_name,
                                          @RequestParam(value = "order_by", required = false)String order_by) {
        Map<String, Object> filters = new HashMap<String, Object>();
        filters.put("id_num", id_num);
        filters.put("add_time_start", add_time_start);
        filters.put("add_time_end", add_time_end);
        filters.put("date_start", date_start);
        filters.put("date_end", date_end);
        filters.put("status", status);
        filters.put("department_id", department_id);
        filters.put("job_id", job_id);
        filters.put("user_name", user_name);
        filters.put("order_by", order_by);
        return Response.success(salaryService.getAll(page_no, page_size, filters));
    }

    @ApiOperation("导出所有记录")
    @GetMapping("/exportSalary")
    public void exportSalary(HttpServletResponse response, @RequestParam(value = "id_num", required = false)String id_num,@ApiParam("记录的最小添加时间")@RequestParam(value = "time_start", required = false)String add_time_start,@ApiParam("记录的最大添加时间")@RequestParam(value = "time_end", required = false)String add_time_end,
                      @ApiParam("记录的最小所属日期")@RequestParam(value = "date_start", required = false)String date_start,@ApiParam("记录的最大所属日期")@RequestParam(value = "date_end", required = false)String date_end,@ApiParam(hidden = true)@RequestParam(value = "status", required = false)Integer status,
                      @RequestParam(value = "department_id", required = false)Integer department_id,@RequestParam(value = "job_id", required = false)Integer job_id,@RequestParam(value = "user_name", required = false)String user_name,
                      @RequestParam(value = "order_by", required = false)String order_by) throws Exception {
        Map<String, Object> filters = new HashMap<String, Object>();
        filters.put("id_num", id_num);
        filters.put("add_time_start", add_time_start);
        filters.put("add_time_end", add_time_end);
        filters.put("date_start", date_start);
        filters.put("date_end", date_end);
        filters.put("status", status);
        filters.put("department_id", department_id);
        filters.put("job_id", job_id);
        filters.put("user_name", user_name);
        filters.put("order_by", order_by);
        salaryService.exportRecords(response, filters, true);
    }

    @ApiOperation("导入记录")
    @PostMapping("/importSalary")
    public Map<String, Object> importSalary(@RequestParam("record_excel") MultipartFile file, @ApiParam(value = "记录的所属日期", required = true) @RequestParam("date")long date) throws IOException {
        if(file.isEmpty()) {
            throw exceptionManager.create("File001");
        }
        if(!file.getContentType().contains("sheet")) {
            throw exceptionManager.create("File002");
        }
        if(500 * 1024 < file.getSize()){
            throw exceptionManager.create("File003");
        }
        salaryService.importRecords(file, new Date(date));
        return Response.success("上传成功");
    }
}
